package demo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.Range;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import javax.validation.constraints.Pattern;
import java.sql.Date;

/**
 * 学生对象，与Student表对应
 */
@Entity
public class Student {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(nullable = false)
    @NotBlank(message = "学院代码不能为空")
    private String collegeId;

    @JsonIgnore // 转为json时忽略此属性
    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "collegeId", referencedColumnName = "id", insertable = false, updatable = false)
    private College college;

    @NotBlank(message = "学号不能为空")
    @Pattern(regexp = "^20\\d{10}$", message = "学号必修是20开头的12位数字")
    private String no;

    @NotBlank(message = "姓名不能为空")
    @Length(min = 2, max = 20, message = "姓名2-20个字符")
    private String name;

    @NotNull(message = "年龄不为空")
    @Range(min = 5, max = 60, message = "年龄必须在5-60之间")
    private int age;

    @NotNull
    @Past(message = "出生日期必须在过去")
    private Date birthday;

    @Lob
    @JsonIgnore // 转为json时忽略此属性
    @Basic(fetch = FetchType.LAZY) // 懒加载策略
    private byte[] photo;

    public Long getId() {
        return id;
    }

    public void setId(Long studentId) {
        this.id = studentId;
    }

    public String getCollegeId() {
        return collegeId;
    }

    public void setCollegeId(String collegeId) {
        this.collegeId = collegeId;
    }

    public College getCollege() {
        return college;
    }

    public void setCollege(College college) {
        this.college = college;
    }

    public String getCollegeName() {
        return college.getName();
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public byte[] getPhoto() {
        return photo;
    }

    public void setPhoto(byte[] photo) {
        this.photo = photo;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", no='" + no + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
